var Modal = function(settings){
	this.id			= '#modal';
	this.width 		= 300;
	this.height 	= 200;
	this.title 		= "Simple Modal";
	this.msg 		= "html messenger";
	this.mask		= false;
	this.maskClose 	= false;
	this.maskShow 	= false;
	this.type 		= 'default';
	this.winH 		= $(window).height();
	this.winW 		= $(window).width();
	
	if(settings.id)
		this.id = settings.id;
	if(settings.width)
		this.width = settings.width;
	if(settings.height)
		this.height = settings.height;
	if(settings.title)
		this.title = settings.title;
	if(settings.msg)
		this.msg = settings.msg;
	if(settings.mask)
		this.mask = settings.mask;
	if(settings.maskClose)
		this.maskClose = settings.maskClose;
	if(settings.type)
		this.type = settings.type;
	
	switch(this.type){
		case 'default':
			console.log('default');
		break;
		case 'loading':
			$('.close').css({
				'display' : "none"
			});
		break;
		case 'alert':
			console.log('alert');
		break;
		case 'confirm':
			console.log('confirm');
		break;
		case 'mensagem':
			console.log('mensagem');
		break;
	}
	this.md = this;	
};

Modal.prototype.closeModal = function(){
	if(this.mask){
		$('#'+idMask).hide();
	}
	$(this.id).hide();
};

Modal.prototype.showMask = function(){	
	
	md = this.md;
	
	if(this.mask){
		// ID do Elemento Mascara
		idMask = 'mask';
		// Adiciona a Div no DOM
		$('body').append('<div id="'+idMask+'"></div>');

		// Define largura e altura do div#mask iguais ás dimensões da tela
		$('#'+idMask).css({
			'width' 			: this.winW,
			'height' 			: this.winH,
			'position' 			: 'fixed',
			'z-index'			: 9000,
			'background-color'	: '#000',
			'left'				: 0,
			'top'				: 0
		});
		
		// efeito de transição
		$('#'+idMask).fadeIn(1000);
		$('#'+idMask).fadeTo("slow", 0.8);
		
		maskShow = true;
		
		if(this.maskClose){
			$('#'+idMask).click(function(){
				md.closeModal();
			});
		}
		
	}
};

Modal.prototype.showModal = function(){
	
	md = this.md;
	
	this.showMask();
	divH = $(this.id).height();
	divW = $(this.id).width();

	// centraliza na tela a janela popup
	$(this.id).css({
		position: 'fixed',
	     top: 	(this.winH / 2) - (divH / 2),
	     left: 	(this.winW / 2) - (divW / 2),
	});
	
	// efeito de transição
	$(this.id).fadeIn(1000);
	
	$('.close').click(function(){
		md.closeModal();
	});
	
	
	$(window).resize(function() {
		
		this.winH = $(window).height();
		this.winW = $(window).width();
		
		divH = $(md.id).height();
		divW = $(md.id).width();
		
		if(maskShow){
			$('#'+idMask).css({
				'width' 			: this.winW,
				'height' 			: this.winH,
			});
		}
		
		$(md.id).css({
			position: 'fixed',
		     top: 	(this.winH / 2) - (divH / 2),
		     left: 	(this.winW / 2) - (divW / 2),
		});
	});
	
	
};

ft = {};

ft.service = function(classe, metodo, args, success, modal) {
	$.ajax({
		type : "POST",
		url : '/' + classe + '/' + metodo,
		data : {
			json : JSON.stringify(args)
		},
		dataType : "json",
		beforeSend : function(x) {
			if (modal) {
				modal.showModal();	
			}
			if (x && x.overrideMimeType) {
				x.overrideMimeType("application/j-son;charset=UTF-8");
			}
		},
		success : function(ret) {
			success(ret);
		},
		error : function(err) {
			console.log(err);
		},
		complete : function() { // Executado depois do success ou error
			if (modal) {
				setTimeout(function(){
					modal.closeModal();
				}, 1000);
			}
		}
	});
};

ft.ResetForm = function(form){
    $(form).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'email':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });
};

ft.redirect = function(url){
	location.href=url;
};



